home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / utils / stlogin4.lzh / WRITE.C < prev    next >
C/C++ Source or Header  |  1993-07-02  |  3KB  |  134 lines

  1. /* UNIX like write program. Kees Lemmens; Juli 1992
  2.  
  3.    This programs can be used to communicate between two login sessions
  4.    on a multitasking ATARI ST (MINT).
  5.    It behaves just like the UNIX write utility.
  6.  
  7.    Bugs/remarks:
  8. -  I don't know how to determine the ttyname, so at this moment I use a
  9.    variable TTYNAME defined by GETTY. If not found then ttyname will be
  10.    "console". LOGNAME will be "root" if this variable is not found.
  11. -  Some programs - like tcsh - send a signal TTOU when a foreign process
  12.    attempts to write to their tty. This causes the foreign process to be
  13.    suspended. To avoid this problem the write program has to ignore such
  14.    signals.
  15. -  Because I don't want to rely upon a UTMP file, you can also write to
  16.    devices instead of users by using the -l option.
  17.  
  18.    Any questions or suggestions about this program can be send to:
  19.    lemmens@dv.twi.tudelft.nl
  20. */
  21.  
  22. #include <stdio.h>
  23. #include <fcntl.h>    /* O_WRONLY etc. */
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include "ux_misc.h"
  28. #include <utmp.h>
  29. #include <signal.h>
  30.  
  31. void set_bell(void)
  32. {    long stack=0;
  33.     char *conterm=(void *)0x484;    /* CON attribs (bell, keyclick) */
  34.  
  35.     if(Super((void *)1L)==0)
  36.         stack=Super((void *)0L);    /* supervisor mode */
  37.     *conterm |= 4;                    /* set bell attribute */
  38.     if(stack) Super((void *)stack);    /* back to user mode  */
  39. }
  40.  
  41. void fatal(char *txt)
  42. {    fprintf(stderr,"WRITE: %s\n",txt);
  43.     exit(1);
  44. }
  45.  
  46. void do_write(char *totty)
  47. {    int ttyfp;
  48.     time_t sec;
  49.     char c,warning[250],fulltty[100];
  50.     char *name,*mytty,*endmsg="\r\n<EOT>\r\n";
  51.  
  52.     set_bell();
  53.     time(&sec);
  54.  
  55.     if((name  = getenv("LOGNAME")) == NULL)    name  = "root";
  56.     if((mytty = getenv("TTYNAME")) == NULL) mytty = "console";
  57.     
  58.     mk_devnm(fulltty,totty);
  59.     if((ttyfp=open(fulltty,O_WRONLY)) < 0)
  60.         fatal("Can't open tty");
  61.  
  62.     sprintf(warning,"\7\r\n\tMessage from %s (%s) [ %.19s ] ...\r\n\7",
  63.         name,mytty,ctime(&sec));
  64.     write(ttyfp,warning,strlen(warning));
  65.     puts("\7Write your message (Ctrl-D to stop)\7");
  66.     
  67.     while(read(1,&c,1),c != '\4' && c != '\32')
  68.     {    write(ttyfp,&c,1);
  69.         if(c=='\r')
  70.         {    write(ttyfp,"\n",1);
  71.             putchar('\n');
  72.         }
  73.     }
  74.     write(ttyfp,endmsg,strlen(endmsg));
  75.     close(ttyfp);
  76. }
  77.  
  78. void usage(void)
  79. {    puts("\nUsage: write [-l <line>] | [<user>]");
  80.     exit(1);
  81. }
  82.  
  83. void name2tty(char *name,char *totty)
  84. {    extern int utmp_fd;
  85.     int fl=0;
  86.     struct utmp *w;
  87.     
  88.     setutent();
  89.     
  90.     while((w=getutent()) != NULL && !fl)
  91.     {    if(!strncmp(w->ut_name,name,sizeof(w->ut_name)) &&
  92.             w->ut_type == USER_PROCESS)
  93.         {    if(Pkill(w->ut_pid,SIGNULL) == 0) /* process exists ? */
  94.             {    strncpy(totty,w->ut_line,sizeof(w->ut_line));
  95.                 fl=1;
  96.             }
  97.             else
  98.             {    w->ut_type=DEAD_PROCESS;
  99.                 setutent();        /* rewind and */
  100.                 pututline(w);    /* correct UTMP entry */
  101.             }
  102.         }
  103.     }
  104.     endutent();
  105.     if(!fl)
  106.     {    puts("User is not logged on");
  107.         exit(0);
  108.     }    
  109. }
  110.  
  111. void main(int argc,char *argv[]) 
  112. {    char totty[100];
  113.     int lfl=0;
  114.     
  115. #ifdef MINT /* ignore signal [Attempted write to foreign tty] */
  116.     signal(SIGTTOU,SIG_IGN);
  117. #endif    
  118.  
  119.     if(argc<2)    usage();
  120.     
  121.     while(--argc>0)            /* parse options */
  122.     {    if(*argv[1]=='-')
  123.         {    switch(*(++argv[1]))
  124.             {    case 'l':    lfl=1;    break;
  125.                 default :    usage();
  126.             }
  127.             ++argv;
  128.         }
  129.     }    
  130.     if(lfl)    strcpy(totty,argv[1]);
  131.     else    name2tty(argv[1],totty);
  132.     
  133.     do_write(totty);
  134. }